<?php
namespace Macmanage\Controller;
use Common\Controller\AdminbaseController;

class MaclistadminController extends AdminbaseController{
	function _initialize() {
		parent::_initialize();
						
	}
	function index(){		
		$macmanage_model = D("Macmanage"); //分页使用由此开始，必须先实例化model，可以不用实例Page类。		
		$count = $macmanage_model->count();		
		$Page  = $this->Page($count,15);  // 实例化分页类 传入总记录数和每页显示的记录数(15)
		$show  = $Page->show("Admin");   // 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$lists  = $macmanage_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();//当用户是超级管理员时，显示所有设备信息
		$lists_count= $macmanage_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->count();//总记录数		
		//dump($lists);			
		$sbtype = M('Sbtype');
		$gtype = $sbtype->select();	
		$sbjg = M('Sbjg');
		$gjg = $sbjg->select();			
		$this -> assign('gjg',$gjg);
		$this -> assign('lists_count',$lists_count); //输出总记录数		
		$this -> assign('gtype',$gtype);	
		$this->assign('lists',$lists);  // 赋值数据集
		$this->assign('Page',$show);   // 赋值分页输出				
		$this->display();     // 输出模板		            
	}
	
	//报修设备
		function badlist(){
		//$lists = $this->smanage_model->order(array("id"=>"asc"))->select();
		//dump($lists);
		//$this->assign('lists',$lists);
		//dump($_SESSION);
		//dump($_SESSION['user']['user_nicename']);
		$admin_name = $_SESSION['name'];//取当前用户的session名字；
		//dump($admin_name);
		$admin_info = D("Users")->where(array("user_login"=>$admin_name))->find();//根据名字从usernas表拿到当前用户信息；
		$admin_nicename = $admin_info['user_nicename'];	//取到该用户的昵称；
		//dump($admin_nicename);	
		$smanage_model = D("Smanage"); //分页使用由此开始，必须先实例化model，可以不用实例Page类。
		if($admin_nicename==='admin'){
			$count = $smanage_model->where(array("sbzt"=>"故障"))->count();
		}else{
				$count = $smanage_model->where(array("sydw"=>$admin_nicename,"sbzt"=>"故障"))->count();  // 查询满足要求的总记录数	
			}		
		$Page  = $this->Page($count,15);  // 实例化分页类 传入总记录数和每页显示的记录数(15)
		$show  = $Page->show("Admin");   // 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		if($admin_nicename==='admin'){
			$lists  = $smanage_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->where(array("sbzt"=>"故障"))->select();//当用户是超级管理员时，显示所有设备信息
			$lists_count= $smanage_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->where(array("sbzt"=>"故障"))->count();//总记录数
		}else{
			$lists  = $smanage_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->where(array("sydw"=>$admin_nicename,"sbzt"=>"故障"))->select();//当是普通管理员时，用昵称与使用单位进行比较，只显示所在机构的设备信息；
			$lists_count = $smanage_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->where(array("sydw"=>$admin_nicename,"sbzt"=>"故障"))->count();//总记录数
			}
		//dump($lists);	
		$sbtype = M('Sbtype');
		$gtype = $sbtype->select();	
		$sbjg = M('Sbjg');
		$gjg = $sbjg->select();			
		$this -> assign('gjg',$gjg);
		$this -> assign('lists_count',$lists_count); //输出总记录数		
		$this -> assign('gtype',$gtype);	
		$this->assign('lists',$lists);  // 赋值数据集
		$this->assign('Page',$show);   // 赋值分页输出				
		$this->display();             // 输出模板
	}
	
	//报废设备
		function baofeilist(){
		//$lists = $this->smanage_model->order(array("id"=>"asc"))->select();
		//dump($lists);
		//$this->assign('lists',$lists);
		//dump($_SESSION);
		//dump($_SESSION['user']['user_nicename']);
		$admin_name = $_SESSION['name'];//取当前用户的session名字；
		//dump($admin_name);
		$admin_info = D("Users")->where(array("user_login"=>$admin_name))->find();//根据名字从usernas表拿到当前用户信息；
		$admin_nicename = $admin_info['user_nicename'];	//取到该用户的昵称；
		//dump($admin_nicename);	
		$smanage_model = D("Smanage"); //分页使用由此开始，必须先实例化model，可以不用实例Page类。
		if($admin_nicename==='admin'){
			$count = $smanage_model->where(array("sbzt"=>"报废"))->count();
		}else{
				$count = $smanage_model->where(array("sydw"=>$admin_nicename,"sbzt"=>"报废"))->count();  // 查询满足要求的总记录数	
			}		
		$Page  = $this->Page($count,15);  // 实例化分页类 传入总记录数和每页显示的记录数(15)
		$show  = $Page->show("Admin");   // 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		if($admin_nicename==='admin'){
			$lists  = $smanage_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->where(array("sbzt"=>"报废"))->select();//当用户是超级管理员时，显示所有设备信息
			$lists_count= $smanage_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->where(array("sbzt"=>"报废"))->count();//总记录数
		}else{
			$lists  = $smanage_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->where(array("sydw"=>$admin_nicename,"sbzt"=>"报废"))->select();//当是普通管理员时，用昵称与使用单位进行比较，只显示所在机构的设备信息；
			$lists_count = $smanage_model->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->where(array("sydw"=>$admin_nicename,"sbzt"=>"报废"))->count();//总记录数
			}
		//dump($lists);	
		$sbtype = M('Sbtype');
		$gtype = $sbtype->select();	
		$sbjg = M('Sbjg');
		$gjg = $sbjg->select();			
		$this -> assign('gjg',$gjg);
		$this -> assign('lists_count',$lists_count); //输出总记录数		
		$this -> assign('gtype',$gtype);	
		$this->assign('lists',$lists);  // 赋值数据集
		$this->assign('Page',$show);   // 赋值分页输出				
		$this->display();             // 输出模板
	}
	
	function ssearch(){
		$typename = I("post.type");
		$jg = I("post.jg");
		$keyword = I("post.keyword");
		//dump($keyword);
		//dump($typename);
		$macmanage_model = D("Macmanage");
		if($typename){
			$ssearchlists = $macmanage_model->where(array("sblb"=>$typename))->select();		
			//dump($ssearchlists);
			$ssearchcount = $macmanage_model->where(array("sblb"=>$typename))->count();
			//dump($ssearchcount);
		}
		if($jg){
			$ssearchlists = $macmanage_model->where(array("sydw"=>$jg))->select();
			$ssearchcount = $macmanage_model->where(array("sydw"=>$jg))->count();	
		}
		if($keyword){
			//dump($keyword);						
			$ssearchlists = $macmanage_model->where(array("sbsn"=>$keyword))->select();			
		}		
		$this -> assign('ssearchlists',$ssearchlists);
		$this -> assign('ssearchcount',$ssearchcount);		
		$this -> display();		
	}
	
	function add(){
		//$admin_id = sp_get_current_admin_id();
		//echo $admin_id;
		$admin_name = $_SESSION['name'];//取当前用户的session名字；
		//dump($admin_name);
		$admin_info = D("Users")->where(array("user_login"=>$admin_name))->find();//根据名字从usernas表拿到当前用户信息；
		//dump($admin_info);
		$this -> assign('admin_info',$admin_info);
		$sbtype = M('Sbtype');
		$gtype = $sbtype->select();
		$sbjg = M('Sbjg');
		$gjg = $sbjg->select();		
		$this -> assign('gtype',$gtype);
		$this -> assign('gjg',$gjg);
		$this->display();		
	}
	function add_post(){
			$macmanage_model = D('Macmanage');
			if(IS_POST){
				if(!empty($_POST['photos_alt']) && !empty($_POST['photos_url'])){
				foreach ($_POST['photos_url'] as $key=>$url){
					$photourl=sp_asset_relative_url($url);
					$_POST['smeta']['photo'][]=array("url"=>$photourl,"alt"=>$_POST['photos_alt'][$key]);
				}
				}				
				$data=I("post.");  //获取所有post数据的正确写法
				$data['smeta']=json_encode($_POST['smeta']);				
				if(!$macmanage_model->create($data)){
					$this -> error($macmanage_model->geterror());//添加有自动验证规则时，应在此处进行错误提示和判断，不能在最后$result处写，否则报不出来错误！					
				}else{
					$result = $macmanage_model->add();
					if($result!==false){
						$this->success("添加成功！",U('Maclistadmin/index'));
					}else{
						$this->error("添加失败！");
					}
					
				}
			}
		}
		
		function edit(){
			$sbtype = M('Sbtype');
			$gtype = $sbtype->select();
			$sbjg = M('Sbjg');
			$gjg = $sbjg->select();		
			$this -> assign('gtype',$gtype);
			$this -> assign('gjg',$gjg);
			
			$id = intval(I("get.id"));
			//dump($id);
			$macinfo = D("Macmanage")->where(array("id"=>$id))->find();					
			$this -> assign('macinfo',$macinfo);									
			$this -> display();
			//dump($_POST);
			
		}
		
		function edit_post(){
			$id = intval(I("post.id"));
			//dump($id);
			if(!empty($_POST['photos_alt']) && !empty($_POST['photos_url'])){
				foreach ($_POST['photos_url'] as $key=>$url){
					$photourl=sp_asset_relative_url($url);
					$_POST['smeta']['photo'][]=array("url"=>$photourl,"alt"=>$_POST['photos_alt'][$key]);
				}
			}
			$data = I("post.");//获取所有post数据；
			$data['smeta']=json_encode($_POST['smeta']);
			$macmanage_model = D('Macmanage');
			if(!$macmanage_model->create($data)){
				$this->error($macmanage_model->geterror());//添加有自动填充规则时，应在此处进行错误提示和判断，不能在最后$result处写，否则报不出来错误！
			}else{
				$result = D("Macmanage")->where(array("id"=>$id))->save();
			}			
			//dump($result);
			if($result!==false){
				$this->success("修改成功！",U('Maclistadmin/index'));
			}else{
				$this->error("修改失败！");
			}
		}
		
		function delete(){
			if(isset($_GET['id'])){
			$id = intval(I('get.id'));
			$result = D("Macmanage")->where(array("id"=>$id))->delete();
			if($result!==false){
				$this->success("删除成功！", U("Maclistadmin/index"));
				}else{
				$this->error('删除失败！');
			}
			}
			if(isset($_POST['ids'])){
			$ids=join(",",$_POST['ids']);
			$result = D("Macmanage")->where("id in ($ids)")->delete();			
			if ($result!==false) {
				$this->success("删除成功！",U("Maclistadmin/index"));
			} else {
				$this->error("删除失败！");
			}
			}
			
		}
		function showinfo(){
			$id = intval(I('get.id'));
			$macmanage_model = D('Macmanage');
			$showinfo = $macmanage_model->where(array("id"=>$id))->find();
			$this -> assign('showinfo',$showinfo);			
			$this->display();
			//dump($showinfo['id']);
			
		}
	
		
}